﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoPartsSearch.Domain.Abstract;
using AutoPartsSearch.Domain.Common;
using AutoPartsSearch.Domain.Entities;
using AutoPartsSearch.Domain.Entities.Messages;

namespace AutoPartsSearch.Domain.Concrete
{
    public class MessageBuilder : IMessageBuilder
    {
        private readonly IConfigurationGetter _configurationGetter;

        public MessageBuilder(IConfigurationGetter configurationGetter)
        {
            this._configurationGetter = configurationGetter;
        }

        public Message BuildSearchQuery(SearchQuery searchQuery, Func<int, string> urlBuilder)
        {
            var subject = _configurationGetter.GetConfigurationValue(Constants.SearchQueryMessageSubject);
            var body = string.Format(_configurationGetter.GetConfigurationValue(Constants.SearchQueryMessageBody)
                , searchQuery.PartName
                , searchQuery.CarBrand.Name.Default
                , searchQuery.CarModel
                , searchQuery.CarYear
                , urlBuilder(searchQuery.Id));
            var from = _configurationGetter.GetConfigurationValue(Constants.FromEmail);
            return new Message
            {
                Body = body,
                Subject = subject,
                From = from
            };
        }

        //Стоимость Вашего запроса на запчасть {0} на {1} {2} {3}г. - {4} {5}.
        // Комментарий продавца: {6}.
        // Информация о продавце:
        //              Название: {7}
        //              Email: {8}
        //              Сервис код: {9}
                      //Телефон: {10}
                      //Телефон #2: {13}
                      //Мобильные телефоны: {14}
                      //Адрес: {11}
                      //Сайт: {12}
        public Message BuildOffer(SearchQuery searchQuery, Seller seller, Offer offer)
        {
            var subject = _configurationGetter.GetConfigurationValue(Constants.SellerOfferMessageSubject);
            var to = searchQuery.Email;
            var from = _configurationGetter.GetConfigurationValue(Constants.FromEmail);
            var body = string.Format(_configurationGetter.GetConfigurationValue(Constants.SellerOfferMessageBody)
                                     , searchQuery.PartName
                                     , searchQuery.CarBrand.Name.Default
                                     , searchQuery.CarModel
                                     , searchQuery.CarYear
                                     , offer.Price
                                     , offer.Currency
                                     , offer.Comments
                                     , seller.CompanyName
                                     , seller.EMail
                                     , seller.Code
                                     , seller.Phone
                                     , seller.Adress
                                     , seller.Site ?? string.Empty
                                     , seller.Phone2
                                     , seller.MobilePhone
                );
            if (!string.IsNullOrEmpty(seller.Sign))
            {
                body += seller.Sign;
            }

            return new Message
            {
                From = from,
                Subject = subject,
                To = to,
                Body = body
            };
        }

        public Message BuildResetPasswordMessage(string email, string newPassword)
        {
            var subject = _configurationGetter.GetConfigurationValue(Constants.ResetPasswordMessageSubject);
            var to = email;
            var from = _configurationGetter.GetConfigurationValue(Constants.FromEmail);
            var body = string.Format(_configurationGetter.GetConfigurationValue(Constants.ResetPasswordMessageBody)
                                     , newPassword);

            return new Message
            {
                From = from,
                Subject = subject,
                To = to,
                Body = body
            };
        }

        public Message BuildContactMessage(Contact contact)
        {
            var subject = Constants.ContactSubject;
            var to = _configurationGetter.GetConfigurationValue(Constants.FromEmail);
            var from = contact.Email;
            var body = string.Format(Constants.ContactBody, contact.Name, contact.Email, contact.Phone, contact.Comments);
            return new Message
                       {
                           From = from,
                           Subject = subject,
                           To = to,
                           Body = body
                       };
        }
    }
}
